package common.spring.auditlog.controller;


import common.spring.auditlog.entity.AuditLog;
import common.spring.auditlog.service.AuditLogService;
import common.spring.auditlog.service.DeviceSensorService;
import common.spring.auditlog.utils.EmailTool;
import common.spring.auditlog.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
public class EmailController {
    @Autowired
    private EmailTool emailTool;
    @Autowired
    private AuditLogService auditLogServiceImpl;


    @RequestMapping(value="/showDtl")
    public String showDtl(Model model)  throws Exception{
        //创建标题
        String title="消防报警明细";
        //查询当天异常记录到List<User> list
        List<AuditLog> list=new ArrayList();
        list=auditLogServiceImpl.selectAll();
        //浏览器显示
        model.addAttribute("url", "http://localhost:18092/show");
        model.addAttribute("list", list);
        return "auditLog";
    }

    @RequestMapping(value="/showEmail")
    public String sendSimpleMail(Model model)  throws Exception{
        //创建标题
        String title="火灾报警";
        //查询当天异常记录到List<User> list
        List<AuditLog> list=new ArrayList();
        list=auditLogServiceImpl.selectToDay();

        //发送Email
        String emailTemplate="auditLog";
        String[] filePath = new String[]{"D:\\jyj.jpg"};
        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("url","http://localhost:18092/show");
        valueMap.put("from","2672205497@qq.com");
        valueMap.put("to", new String[]{"2672205497@qq.com"});
        valueMap.put("title", title);
        valueMap.put("list", list);
        valueMap.put("filePathList", filePath);
        valueMap.put("emailTemplate", emailTemplate);
        emailTool.sendSimpleMail(valueMap);
        //发送微信
        String openid = "oVOI-53ZSCNqOrwqqt4B7nAyENUE";
        StringBuffer message =new StringBuffer("") ; //今日截止当前总共"+list.size()+"记录 "</br>"
        for (AuditLog each:list) {
            message.append(each.getLogNum()+" "+each.getLogData()+" "+each.getLogType()+" "+each.getLogTableName()+" "+each.getSrcNum()+" "+each.getCreateDt());
        }
        HttpClientUtils.sendPost("http://192.168.1.108:8080/ex/send/message","toUser="+openid+"&message="+message.toString());
        //浏览器显示
        model.addAttribute("url", "http://127.0.0.1:18092/showDtl");
        model.addAttribute("list", list);
        return "auditLog";
    }


}

